package cz.muni.fi.pa165.languageschool.rest.client.app;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import javax.ws.rs.core.Response;

/**
 * Auxiliary class which is used for manipulation with string
 *
 * @author Marketa Trachtova (255498) marketa.tr(at)gmail.com
 */
class Helper {

    private static BufferedReader input = new BufferedReader(new InputStreamReader(System.in));
    final static String NL = System.getProperty("line.separator");

    public static void clear() {
        System.out.println("------------------------------");
        System.out.print(NL + NL + NL);
    }

    public static Object getResponse(String message, Object... expected) {
        boolean number = false;
        boolean string = false;

        HashSet<String> strings = new HashSet<>();

        for (Object o : expected) {
            if (o.equals(String.class)) {
                string = true;
            } else if (o instanceof String) {
                strings.add((String) o);
            }
            if (o.equals(Integer.class)) {
                number = true;
            }
        }

        System.out.print(message);
        int repeat = 15;
        while (repeat > 0) {
            repeat--;
            System.out.print(": ");
            try {
                String line = input.readLine();
                if (strings.contains(line)) {
                    return line;
                }
                if (number) {
                    try {
                        return Integer.valueOf(line);
                    } catch (NumberFormatException ex) {
                        // nothing
                    }
                }
                if (string && !line.isEmpty()) {
                    return line;
                }
            } catch (IOException ex) {
                Helper.log(ex);
                Runtime.getRuntime().exit(1);
            }
        }
        return null;
    }

    public static String setParam(String paramName) {
        Object param = Helper.getResponse("Set " + paramName + ":", String.class);
        String regex = "^[a-zA-Z0-9]+$";
        String paramContent = (String) param;

        if (!paramContent.matches(regex)) {
            System.out.println("Wrong input: " + paramName + " can consist only of alphanumeric characters without white spaces");
            param = Helper.getResponse("Set " + paramName + ":", String.class);
            paramContent = (String) param;
        }
        String resp = "\"" + paramName + "\":\"" + paramContent + "\"";
        return resp;
    }

    public static String getParam(String paramName) {
        Object param = Helper.getResponse("Set " + paramName + ":", String.class);
        String regex = "^[a-zA-Z0-9]+$";
        String paramContent = (String) param;

        if (!paramContent.matches(regex)) {
            System.out.println("Wrong input: " + paramName + " can consist only of alphanumeric characters without white spaces");
            param = Helper.getResponse("Set " + paramName + ":", String.class);
        }
        String resp = "\"" + paramContent + "\"";
        return resp;
    }

    public static Map<String, String> toMap(String string) {
        Map<String, String> map = new HashMap<>();
        String[] tokens;
        String[] tokens2;

        string = string.replaceAll("\\{|\\}", ""); //get rid of { and }
        tokens = string.split(","); //spli string to 7 parts devided by ,

        //create hash map from string
        for (int j = 0; j < tokens.length;) {
            tokens2 = tokens[j].split("\\:");
            for (int i = 0; i < tokens2.length - 1;) {
                map.put(tokens2[i++], tokens2[i++]);
            }
            j++;
        }

        return map;
    }

    public static String prinStringResponse(Response response) {
        String resp = response.readEntity(String.class);
        String raw = resp;

        resp = resp.replaceAll("[\'\"]", "");
        resp = resp.replace("\"type\":\"courseResource\"", "");
        resp = resp.replaceAll("\\{|\\}", "");
        for (String s : resp.split("\\,")) {
            System.out.println(s);
        }
        return raw;

    }

    private static void log(Exception ex) {
        System.out.println("Server communication problem.");
    }
}
